package com.cloudcreativity.blyg.ui;

import android.content.Intent;
import android.content.IntentFilter;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;

import com.cloudcreativity.blyg.R;
import com.cloudcreativity.blyg.base.BaseActivity;
import com.cloudcreativity.blyg.databinding.ActivityMainBinding;
import com.cloudcreativity.blyg.receiver.MyBusinessReceiver;
import com.cloudcreativity.blyg.ui.cashier.CashierAC;
import com.cloudcreativity.blyg.utils.ScanGunHelper;

public class MainActivity extends BaseActivity {

    private MyBusinessReceiver receiver;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        receiver = new MyBusinessReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(MyBusinessReceiver.ACTION_LOGOUT);
        filter.addAction(MyBusinessReceiver.ACTION_EXIT_APP);
        filter.addAction(MyBusinessReceiver.ACTION_RE_LOGIN);



        registerReceiver(receiver,filter);

        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setModel(new MainModel(this,binding,this));
    }

    // 注册扫码抢事件
    ScanGunHelper scanGunHelper = new ScanGunHelper(new ScanGunHelper.OnScanValueListener() {
        @Override
        public void onScanValue(String value) {
            //现在扫码跳转到收银页面直接
            Intent intent = new Intent(MainActivity.this, CashierAC.class);
            intent.putExtra("scanCode",value);
            startActivity(intent);
        }
    });

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(receiver!=null){
            unregisterReceiver(receiver);
        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        // LogUtils.e("xuxiwu","device id = "+event.getDeviceId());
        if ((event.getKeyCode() != KeyEvent.KEYCODE_BACK) && (event.getDeviceId()!=-1)) {
            scanGunHelper.analysisKeyEvent(event);
            return true;
        }
        return super.dispatchKeyEvent(event);
    }
}
